#include <iostream>

using namespace std;


int compare(char *str1, char *str2, int length)
{
	for (int i = 0; i < length; i++) {
		if (str1[i] < str2[i]) {
			return -1;
		} else if (str1[i] > str2[i]) {
			return 1;
		}
	}
	return 0;
}

int main(int argc, char *argv[])
{
	const int length = 21;
	const int resultLength = 10000;
	char *result[resultLength];

	int N;
	cin >> N;

	char **dict = new char*[N];
	int i = 0;
	int size = 0;
	while (i < N) {
		char *item = new char[length];
		int j = 0;
		for (; j < length; j++) {
			item[j] = 0;
		}
		cin>>item;
		
		bool isEqual = true;
		if (i == 0) {
			dict[size] = item;
			++size;
		} else {
			int k = 0, l = 0; 
			for (; k < i; k++) {
				isEqual = true;
				for (l = 0; l < length; l++) {
					if (dict[k][l] != item[l]) {
						isEqual = false;
						break;
					}
				}
				if (isEqual) {
					break;
				}
			}
			if (k < i) {
				delete []item;
			} else {
				dict[size] = item;
				++size;
			}
		}
		++i;		
	}

	int Q;
	cin>>Q;
	
	while (Q > 0) {
		char query[length];
		for (i = 0; i < length; i++) {
			query[i] = 0;
		}
		cin>>query;
		int resultIdx = 0;
		int j = 0;
		for (i = 0; i < size; i++) {
			bool isEqual = true;
			for (j = 0; j< length; j++) {
				if (query[j] == 0)
					break;
				if (dict[i][j] != query[j]) {
					isEqual = false;
					break;
				}
			}
			if (isEqual == true) {
				result[resultIdx] = dict[i];
				resultIdx++;
			}
		}

		if (resultIdx == 0) {
			result[resultIdx++] = query;
			cout<<result[0]<<endl;
		} else {
			// Alphabetical sorting
			for (i = 1; i < resultIdx; i++) {
				char *temp = result[i];
				for (j = i - 1; j >= 0; j--) {
					if (compare(result[j], temp,  length) == 1 ) {
						result[j + 1] = result[j];
					} else if (compare(result[j], temp,  length) == -1) {
						break;
					}
				}
				result[j + 1] = temp;
			}
			int outputNumber = 8;
			for (i = 0; i < outputNumber; i++) {
				cout<<result[i]<< " ";
			}
			cout<<endl;
		}
		--Q;
	}
	for (i = 0; i < resultLength; i++) 
		result[i] = NULL;
	for (i = 0; i < size; i++) {
		delete[] dict[i];
	}
	delete[] dict;
	return 0;
}